TOTP VALIDATION (WITHOUT CREATING CREDENTIAL)

工作流概述

这是一个包含5个节点的中等工作流,主要用于自动化处理各种任务。

工作流源代码

下载
{
  "name": "TOTP VALIDATION (WITHOUT CREATING CREDENTIAL)",
  "nodes": [
    {
      "id": "56f102c4-5b84-4e30-955c-0ea1221c328f",
      "name": "When clicking ‘Test workflow’",
      "type": "n8n-nodes-base.manualTrigger",
      "position": [
        480,
        680
      ],
      "parameters": {},
      "typeVersion": 1
    },
    {
      "id": "4f562819-ee42-42ad-b821-aff2cbebbc0f",
      "name": "TOTP VALIDATION",
      "type": "n8n-nodes-base.code",
      "position": [
        920,
        680
      ],
      "parameters": {
        "language": "python",
        "pythonCode": "import hmac
import hashlib
import time
import base64

def base32_decode(key):
    \"\"\"Decodes a base32 key into bytes\"\"\"
    key += '=' * (-len(key) % 8)  # Add necessary '=' for valid length
    return base64.b32decode(key.upper(), casefold=True)

def generate_totp(secret, interval=30, digits=6):
    \"\"\"Generates a TOTP code based on a secret key\"\"\"
    interval_count = int(time.time() // interval)
    interval_bytes = interval_count.to_bytes(8, byteorder='big')

    hmac_hash = hmac.new(secret, interval_bytes, hashlib.sha1).digest()
    
    offset = hmac_hash[-1] & 0x0F
    binary_code = ((hmac_hash[offset] & 0x7F) << 24 |
                   (hmac_hash[offset + 1] & 0xFF) << 16 |
                   (hmac_hash[offset + 2] & 0xFF) << 8 |
                   (hmac_hash[offset + 3] & 0xFF))
    
    otp_code = binary_code % (10 ** digits)
    
    # Format with leading zeros if necessary
    otp_code_str = str(otp_code).zfill(digits)
    
    return otp_code_str

def verify_totp(secret, code, interval=30, digits=6):
    \"\"\"Checks whether the TOTP code is valid\"\"\"
    secret_bytes = base32_decode(secret)
    generated_code = generate_totp(secret_bytes, interval, digits)
    
    return generated_code == code

# Example of use
secret = _input.item.json.totp_secret_example  # Secret key base32 (example)
code =  _input.item.json.code_to_verify_example # Code to check (example)

# Return 1 if code is valid. Return 0 if invalid
if verify_totp(secret, code):
    return [{\"status\": 1}]
else:
    return [{\"status\": 0}]"
      },
      "typeVersion": 2
    },
    {
      "id": "9760b31c-5ba8-4001-9cbe-2be2ae58d58e",
      "name": "IF CODE IS VALID",
      "type": "n8n-nodes-base.if",
      "position": [
        1140,
        680
      ],
      "parameters": {
        "options": {},
        "conditions": {
          "options": {
            "leftValue": "",
            "caseSensitive": true,
            "typeValidation": "strict"
          },
          "combinator": "and",
          "conditions": [
            {
              "id": "470cf368-daee-4136-b907-a3539765871d",
              "operator": {
                "type": "number",
                "operation": "equals"
              },
              "leftValue": "={{ $json.status }}",
              "rightValue": 1
            }
          ]
        }
      },
      "typeVersion": 2.1
    },
    {
      "id": "3a029863-8fd0-42ef-b8ff-9f7cdf6e8d94",
      "name": "Sticky Note",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        440,
        180
      ],
      "parameters": {
        "width": 883,
        "height": 430,
        "content": "## TOTP Validation with Function Node

This template allows you to verify if a 6-digit TOTP code is valid using the corresponding TOTP secret. It can be used in an authentication system.
### Example usage:
- You retrieve the user's TOTP secret from a database, then you want to verify if the 2FA code provided by the user is valid.

## Setup Guidelines

You only need the \"TOTP VALIDATION\" node.
You will need to modify lines 39 and 40 of the \"TOTP VALIDATION\" node with the correct values for your specific context.

## Testing the Template
You can define a sample secret and code in the \"EXAMPLE FIELDS\" node below, then click \"Test Workflow\".
If the code is valid for the provided secret, the flow will proceed to the \"true\" branch of the \"IF CODE IS VALID\" node. Otherwise, it will go to the \"false\" branch."
      },
      "typeVersion": 1
    },
    {
      "id": "f660a50f-2c33-49bb-b975-8d51e9bf24ed",
      "name": "EXAMPLE FIELDS",
      "type": "n8n-nodes-base.set",
      "position": [
        700,
        680
      ],
      "parameters": {
        "options": {},
        "assignments": {
          "assignments": [
            {
              "id": "03a66bf9-1bf4-44c0-92e0-edd45929e467",
              "name": "code_to_verify_example",
              "type": "string",
              "value": "516620"
            },
            {
              "id": "7bb18b0a-1851-4f27-a91f-5f93b663cfd0",
              "name": "totp_secret_example",
              "type": "string",
              "value": "CNSUKUMZLQJEZJ3"
            }
          ]
        }
      },
      "typeVersion": 3.4
    }
  ],
  "active": false,
  "pinData": {},
  "settings": {
    "executionOrder": "v1"
  },
  "connections": {
    "EXAMPLE FIELDS": {
      "main": [
        [
          {
            "node": "TOTP VALIDATION",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "TOTP VALIDATION": {
      "main": [
        [
          {
            "node": "IF CODE IS VALID",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "When clicking ‘Test workflow’": {
      "main": [
        [
          {
            "node": "EXAMPLE FIELDS",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

功能特点

  • 自动检测新邮件
  • AI智能内容分析
  • 自定义分类规则
  • 批量处理能力
  • 详细的处理日志

技术分析

节点类型及作用

  • Manualtrigger
  • Code
  • If
  • Stickynote
  • Set

复杂度评估

配置难度:
★★★☆☆
维护难度:
★★☆☆☆
扩展性:
★★★★☆

实施指南

前置条件

  • 有效的Gmail账户
  • n8n平台访问权限
  • Google API凭证
  • AI分类服务订阅

配置步骤

  1. 在n8n中导入工作流JSON文件
  2. 配置Gmail节点的认证信息
  3. 设置AI分类器的API密钥
  4. 自定义分类规则和标签映射
  5. 测试工作流执行
  6. 配置定时触发器(可选)

关键参数

参数名称 默认值 说明
maxEmails 50 单次处理的最大邮件数量
confidenceThreshold 0.8 分类置信度阈值
autoLabel true 是否自动添加标签

最佳实践

优化建议

  • 定期更新AI分类模型以提高准确性
  • 根据邮件量调整处理批次大小
  • 设置合理的分类置信度阈值
  • 定期清理过期的分类规则

安全注意事项

  • 妥善保管API密钥和认证信息
  • 限制工作流的访问权限
  • 定期审查处理日志
  • 启用双因素认证保护Gmail账户

性能优化

  • 使用增量处理减少重复工作
  • 缓存频繁访问的数据
  • 并行处理多个邮件分类任务
  • 监控系统资源使用情况

故障排除

常见问题

邮件未被正确分类

检查AI分类器的置信度阈值设置,适当降低阈值或更新训练数据。

Gmail认证失败

确认Google API凭证有效且具有正确的权限范围,重新进行OAuth授权。

调试技巧

  • 启用详细日志记录查看每个步骤的执行情况
  • 使用测试邮件验证分类逻辑
  • 检查网络连接和API服务状态
  • 逐步执行工作流定位问题节点

错误处理

工作流包含以下错误处理机制:

  • 网络超时自动重试(最多3次)
  • API错误记录和告警
  • 处理失败邮件的隔离机制
  • 异常情况下的回滚操作